home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / PrintWriter.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  13.9 KB  |  503 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)PrintWriter.java    1.19 98/06/29
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17.  
  18. /**
  19.  * Print formatted representations of objects to a text-output stream.  This
  20.  * class implements all of the print methods found in PrintStream.  It does not
  21.  * contain methods for writing raw bytes, for which a program should use
  22.  * unencoded byte streams.
  23.  *
  24.  * <p> Unlike the PrintStream class, if automatic flushing is enabled it will
  25.  * be done only when one of the println() methods is invoked, rather than
  26.  * whenever a newline character happens to be output.  The println() methods
  27.  * use the platform's own notion of line separator rather than the newline
  28.  * character.
  29.  *
  30.  * <p> Methods in this class never throw I/O exceptions.  The client may
  31.  * inquire as to whether any errors have occurred by invoking checkError().
  32.  *
  33.  * @version     1.19, 98/06/29
  34.  * @author    Frank Yellin
  35.  * @author    Mark Reinhold
  36.  * @since    JDK1.1
  37.  */
  38.  
  39. public class PrintWriter extends Writer {
  40.  
  41.     /**
  42.      * The underlying character-output stream of this
  43.      * <code>PrintWriter</code>.
  44.      *
  45.      * @since JDK1.2
  46.      */
  47.     protected Writer out;
  48.  
  49.     private boolean autoFlush = false;
  50.     private boolean trouble = false;
  51.  
  52.     /**
  53.      * Line separator string.  This is the value of the line.separator
  54.      * property at the moment that the stream was created.
  55.      */
  56.     private String lineSeparator;
  57.  
  58.     /**
  59.      * Create a new PrintWriter, without automatic line flushing.
  60.      *
  61.      * @param  out        A character-output stream
  62.      */
  63.     public PrintWriter (Writer out) {
  64.     this(out, false);
  65.     }
  66.  
  67.     /**
  68.      * Create a new PrintWriter.
  69.      *
  70.      * @param  out        A character-output stream
  71.      * @param  autoFlush  A boolean; if true, the println() methods will flush
  72.      *                    the output buffer
  73.      */
  74.     public PrintWriter(Writer out,
  75.                boolean autoFlush) {
  76.     super(out);
  77.     this.out = out;
  78.     this.autoFlush = autoFlush;
  79.     lineSeparator = (String) java.security.AccessController.doPrivileged(
  80.                new sun.security.action.GetPropertyAction("line.separator"));
  81.     }
  82.  
  83.     /**
  84.      * Create a new PrintWriter, without automatic line flushing, from an
  85.      * existing OutputStream.  This convenience constructor creates the
  86.      * necessary intermediate OutputStreamWriter, which will convert characters
  87.      * into bytes using the default character encoding.
  88.      *
  89.      * @param  out        An output stream
  90.      *
  91.      * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
  92.      */
  93.     public PrintWriter(OutputStream out) {
  94.     this(out, false);
  95.     }
  96.  
  97.     /**
  98.      * Create a new PrintWriter from an existing OutputStream.  This
  99.      * convenience constructor creates the necessary intermediate
  100.      * OutputStreamWriter, which will convert characters into bytes using the
  101.      * default character encoding.
  102.      *
  103.      * @param  out        An output stream
  104.      * @param  autoFlush  A boolean; if true, the println() methods will flush
  105.      *                    the output buffer
  106.      *
  107.      * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
  108.      */
  109.     public PrintWriter(OutputStream out, boolean autoFlush) {
  110.     this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
  111.     }
  112.  
  113.     /** Check to make sure that the stream has not been closed */
  114.     private void ensureOpen() throws IOException {
  115.     if (out == null)
  116.         throw new IOException("Stream closed");
  117.     }
  118.  
  119.     /** Flush the stream. */
  120.     public void flush() {
  121.     try {
  122.         synchronized (lock) {
  123.         ensureOpen();
  124.         out.flush();
  125.         }
  126.     }
  127.     catch (IOException x) {
  128.         trouble = true;
  129.     }
  130.     }
  131.  
  132.     /** Close the stream. */
  133.     public void close() {
  134.     try {
  135.         synchronized (lock) {
  136.         if (out == null)
  137.             return;
  138.         out.close();
  139.         out = null;
  140.         }
  141.     }
  142.     catch (IOException x) {
  143.         trouble = true;
  144.     }
  145.     }
  146.  
  147.     /**
  148.      * Flush the stream and check its error state.  Errors are cumulative;
  149.      * once the stream encounters an error, this routine will return true on
  150.      * all successive calls.
  151.      *
  152.      * @return True if the print stream has encountered an error, either on the
  153.      * underlying output stream or during a format conversion.
  154.      */
  155.     public boolean checkError() {
  156.     if (out != null)
  157.         flush();
  158.     return trouble;
  159.     }
  160.  
  161.     /** Indicate that an error has occurred. */
  162.     protected void setError() {
  163.     trouble = true;
  164.     }
  165.  
  166.  
  167.     /*
  168.      * Exception-catching, synchronized output operations,
  169.      * which also implement the write() methods of Writer
  170.      */
  171.  
  172.     /** Write a single character. */
  173.     public void write(int c) {
  174.     try {
  175.         synchronized (lock) {
  176.         ensureOpen();
  177.         out.write(c);
  178.         }
  179.     }
  180.     catch (InterruptedIOException x) {
  181.         Thread.currentThread().interrupt();
  182.     }
  183.     catch (IOException x) {
  184.         trouble = true;
  185.     }
  186.     }
  187.  
  188.     /** Write a portion of an array of characters. */
  189.     public void write(char buf[], int off, int len) {
  190.     try {
  191.         synchronized (lock) {
  192.         ensureOpen();
  193.         out.write(buf, off, len);
  194.         }
  195.     }
  196.     catch (InterruptedIOException x) {
  197.         Thread.currentThread().interrupt();
  198.     }
  199.     catch (IOException x) {
  200.         trouble = true;
  201.     }
  202.     }
  203.  
  204.     /**
  205.      * Write an array of characters.  This method cannot be inherited from the
  206.      * Writer class because it must suppress I/O exceptions.
  207.      */
  208.     public void write(char buf[]) {
  209.     write(buf, 0, buf.length);
  210.     }
  211.  
  212.     /** Write a portion of a string. */
  213.     public void write(String s, int off, int len) {
  214.     try {
  215.         synchronized (lock) {
  216.         ensureOpen();
  217.         out.write(s, off, len);
  218.         }
  219.     }
  220.     catch (InterruptedIOException x) {
  221.         Thread.currentThread().interrupt();
  222.     }
  223.     catch (IOException x) {
  224.         trouble = true;
  225.     }
  226.     }
  227.  
  228.     /**
  229.      * Write a string.  This method cannot be inherited from the Writer class
  230.      * because it must suppress I/O exceptions.
  231.      */
  232.     public void write(String s) {
  233.     write(s, 0, s.length());
  234.     }
  235.  
  236.     private void newLine() {
  237.     try {
  238.         synchronized (lock) {
  239.         ensureOpen();
  240.         out.write(lineSeparator);
  241.         if (autoFlush)
  242.             out.flush();
  243.         }
  244.     }
  245.     catch (InterruptedIOException x) {
  246.         Thread.currentThread().interrupt();
  247.     }
  248.     catch (IOException x) {
  249.         trouble = true;
  250.     }
  251.     }
  252.  
  253.  
  254.     /* Methods that do not terminate lines */
  255.  
  256.     /**
  257.      * Print a boolean value.  The string produced by <code>{@link
  258.      * java.lang.String#valueOf(boolean)}</code> is translated into bytes
  259.      * according to the platform's default character encoding, and these bytes
  260.      * are written in exactly the manner of the <code>{@link
  261.      * #write(int)}</code> method.
  262.      *
  263.      * @param      b   The <code>boolean</code> to be printed
  264.      */
  265.     public void print(boolean b) {
  266.     write(b ? "true" : "false");
  267.     }
  268.  
  269.     /**
  270.      * Print a character.  The character is translated into one or more bytes
  271.      * according to the platform's default character encoding, and these bytes
  272.      * are written in exactly the manner of the <code>{@link
  273.      * #write(int)}</code> method.
  274.      *
  275.      * @param      c   The <code>char</code> to be printed
  276.      */
  277.     public void print(char c) {
  278.     write(String.valueOf(c));
  279.     }
  280.  
  281.     /**
  282.      * Print an integer.  The string produced by <code>{@link
  283.      * java.lang.String#valueOf(int)}</code> is translated into bytes according
  284.      * to the platform's default character encoding, and these bytes are
  285.      * written in exactly the manner of the <code>{@link #write(int)}</code>
  286.      * method.
  287.      *
  288.      * @param      i   The <code>int</code> to be printed
  289.      * @see        java.lang.Integer#toString(int)
  290.      */
  291.     public void print(int i) {
  292.     write(String.valueOf(i));
  293.     }
  294.  
  295.     /**
  296.      * Print a long integer.  The string produced by <code>{@link
  297.      * java.lang.String#valueOf(long)}</code> is translated into bytes
  298.      * according to the platform's default character encoding, and these bytes
  299.      * are written in exactly the manner of the <code>{@link #write(int)}</code>
  300.      * method.
  301.      *
  302.      * @param      l   The <code>long</code> to be printed
  303.      * @see        java.lang.Long#toString(long)
  304.      */
  305.     public void print(long l) {
  306.     write(String.valueOf(l));
  307.     }
  308.  
  309.     /**
  310.      * Print a floating-point number.  The string produced by <code>{@link
  311.      * java.lang.String#valueOf(float)}</code> is translated into bytes
  312.      * according to the platform's default character encoding, and these bytes
  313.      * are written in exactly the manner of the <code>{@link #write(int)}</code>
  314.      * method.
  315.      *
  316.      * @param      f   The <code>float</code> to be printed
  317.      * @see        java.lang.Float#toString(float)
  318.      */
  319.     public void print(float f) {
  320.     write(String.valueOf(f));
  321.     }
  322.  
  323.     /**
  324.      * Print a double-precision floating-point number.  The string produced by
  325.      * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
  326.      * bytes according to the platform's default character encoding, and these
  327.      * bytes are written in exactly the manner of the <code>{@link
  328.      * #write(int)}</code> method.
  329.      *
  330.      * @param      d   The <code>double</code> to be printed
  331.      * @see        java.lang.Double#toString(double)
  332.      */
  333.     public void print(double d) {
  334.     write(String.valueOf(d));
  335.     }
  336.  
  337.     /**
  338.      * Print an array of characters.  The characters are converted into bytes
  339.      * according to the platform's default character encoding, and these bytes
  340.      * are written in exactly the manner of the <code>{@link #write(int)}</code>
  341.      * method.
  342.      *
  343.      * @param      s   The array of chars to be printed
  344.      *
  345.      * @throws  NullPointerException  If <code>s</code> is <code>null</code>
  346.      */
  347.     public void print(char s[]) {
  348.     write(s);
  349.     }
  350.  
  351.     /**
  352.      * Print a string.  If the argument is <code>null</code> then the string
  353.      * <code>"null"</code> is printed.  Otherwise, the string's characters are
  354.      * converted into bytes according to the platform's default character
  355.      * encoding, and these bytes are written in exactly the manner of the
  356.      * <code>{@link #write(int)}</code> method.
  357.      *
  358.      * @param      s   The <code>String</code> to be printed
  359.      */
  360.     public void print(String s) {
  361.     if (s == null) {
  362.         s = "null";
  363.     }
  364.     write(s);
  365.     }
  366.  
  367.     /**
  368.      * Print an object.  The string produced by the <code>{@link
  369.      * java.lang.String#valueOf(Object)}</code> method is translated into bytes
  370.      * according to the platform's default character encoding, and these bytes
  371.      * are written in exactly the manner of the <code>{@link #write(int)}</code>
  372.      * method.
  373.      *
  374.      * @param      obj   The <code>Object</code> to be printed
  375.      * @see        java.lang.Object#toString()
  376.      */
  377.     public void print(Object obj) {
  378.     write(String.valueOf(obj));
  379.     }
  380.  
  381.  
  382.     /* Methods that do terminate lines */
  383.  
  384.     /**
  385.      * Terminate the current line by writing the line separator string.  The
  386.      * line separator string is defined by the system property
  387.      * <code>line.separator</code>, and is not necessarily a single newline
  388.      * character (<code>'\n'</code>).
  389.      */
  390.     public void println() {
  391.     newLine();
  392.     }
  393.  
  394.     /**
  395.      * Print a boolean value and then terminate the line.  This method behaves
  396.      * as though it invokes <code>{@link #print(boolean)}</code> and then
  397.      * <code>{@link #println()}</code>.
  398.      */
  399.     public void println(boolean x) {
  400.     synchronized (lock) {
  401.         print(x);
  402.         println();
  403.     }
  404.     }
  405.  
  406.     /**
  407.      * Print a character and then terminate the line.  This method behaves as
  408.      * though it invokes <code>{@link #print(char)}</code> and then <code>{@link
  409.      * #println()}</code>.
  410.      */
  411.     public void println(char x) {
  412.     synchronized (lock) {
  413.         print(x);
  414.         println();
  415.     }
  416.     }
  417.  
  418.     /**
  419.      * Print an integer and then terminate the line.  This method behaves as
  420.      * though it invokes <code>{@link #print(int)}</code> and then <code>{@link
  421.      * #println()}</code>.
  422.      */
  423.     public void println(int x) {
  424.     synchronized (lock) {
  425.         print(x);
  426.         println();
  427.     }
  428.     }
  429.  
  430.     /**
  431.      * Print a long integer and then terminate the line.  This method behaves
  432.      * as though it invokes <code>{@link #print(long)}</code> and then
  433.      * <code>{@link #println()}</code>.
  434.      */
  435.     public void println(long x) {
  436.     synchronized (lock) {
  437.         print(x);
  438.         println();
  439.     }
  440.     }
  441.  
  442.     /**
  443.      * Print a floating-point number and then terminate the line.  This method
  444.      * behaves as though it invokes <code>{@link #print(float)}</code> and then
  445.      * <code>{@link #println()}</code>.
  446.      */
  447.     public void println(float x) {
  448.     synchronized (lock) {
  449.         print(x);
  450.         println();
  451.     }
  452.     }
  453.  
  454.     /**
  455.      * Print a double-precision floating-point number and then terminate the
  456.      * line.  This method behaves as though it invokes <code>{@link
  457.      * #print(double)}</code> and then <code>{@link #println()}</code>.
  458.      */
  459.     public void println(double x) {
  460.     synchronized (lock) {
  461.         print(x);
  462.         println();
  463.     }
  464.     }
  465.  
  466.     /**
  467.      * Print an array of characters and then terminate the line.  This method
  468.      * behaves as though it invokes <code>{@link #print(char[])}</code> and then
  469.      * <code>{@link #println()}</code>.
  470.      */
  471.     public void println(char x[]) {
  472.     synchronized (lock) {
  473.         print(x);
  474.         println();
  475.     }
  476.     }
  477.  
  478.     /**
  479.      * Print a String and then terminate the line.  This method behaves as
  480.      * though it invokes <code>{@link #print(String)}</code> and then
  481.      * <code>{@link #println()}</code>.
  482.      */
  483.     public void println(String x) {
  484.     synchronized (lock) {
  485.         print(x);
  486.         println();
  487.     }
  488.     }
  489.  
  490.     /**
  491.      * Print an Object and then terminate the line.  This method behaves as
  492.      * though it invokes <code>{@link #print(Object)}</code> and then
  493.      * <code>{@link #println()}</code>.
  494.      */
  495.     public void println(Object x) {
  496.     synchronized (lock) {
  497.         print(x);
  498.         println();
  499.     }
  500.     }
  501.  
  502. }
  503.